﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Tasker.Infrastructure.Repositories;
using Tasker.Infrastructure.Services;
using Tasker.Repositories;
using Tasker.Repositories.Repositories;
using Tasker.Services;

namespace Tasker
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel _ninjectKernel;

        public NinjectControllerFactory()
        {
            _ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController) _ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            BindRepositories();
            BindServices();
        }

        private void BindRepositories()
        {
            _ninjectKernel.Bind<ITasksRepository>().To<TasksRepositoryEF>();
            _ninjectKernel.Bind<IUsersRepository>().To<UsersRepositoryEF>();
        }

        private void BindServices()
        {
            _ninjectKernel.Bind<IAccountService>().To<AccountService>();
        }
    }
}